﻿package com.vicentediaz.view.imageselector
{
    import com.greensock.*;
    import com.vicentediaz.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class Thumb extends Sprite
    {
        private var container:Sprite;
        private var _selected:Boolean;
        private var rollover:Sprite;
        private var loader:Shape;
        private var loaderTween:TweenMax;
        public var id:uint;
        private var ox:Number;
        private var oy:Number;
        private var bitmap:Bitmap;
        public const UNSELECTED_ALPHA:Number = 0.4;

        public function Thumb()
        {
            return;
        }// end function

        private function onThumbRollOver(param1:MouseEvent) : void
        {
            TweenMax.to(this.container, 0.2, {blurFilter:{blurX:20, blurY:20}, x:Math.random() * 80, y:Math.random() * 80, scaleX:3, scaleY:3});
            Sounds.getInstance().playById("ROLLOVER_THUMB", 1, (-(stage.stageWidth / 2 - stage.mouseX)) / stage.stageWidth);
            return;
        }// end function

        public function createFromBitmap(param1:BitmapData) : void
        {
            this.container = new Sprite();
            this.bitmap = new Bitmap(param1, "auto", false);
            this.container.addChild(this.bitmap);
            this.bitmap.x = (-this.bitmap.width) * 0.5;
            this.bitmap.y = (-this.bitmap.height) * 0.5;
            this.ox = this.container.width * 0.5;
            this.oy = this.container.height * 0.5;
            this.container.x = this.ox;
            this.container.y = this.oy;
            scrollRect = new Rectangle(0, 0, this.container.width, this.container.height);
            addChild(this.container);
            this.drawLoader();
            this.drawRollover();
            this._selected = false;
            alpha = this.UNSELECTED_ALPHA;
            return;
        }// end function

        public function set progress(param1:Number) : void
        {
            if (this.loaderTween)
            {
                this.loaderTween.updateTo({scaleX:param1}, true);
            }
            else
            {
                this.loaderTween = TweenMax.to(this.loader, 0.1, {scaleX:param1});
            }// end else if
            TweenMax.to(this.loader, 0.2, {alpha:param1 == 0 ? (0) : (1)});
            return;
        }// end function

        private function onThumbRollOut(param1:MouseEvent) : void
        {
            TweenMax.to(this.container, 0.2, {blurFilter:{blurX:0, blurY:0}, x:this.ox, y:this.oy, scaleX:1, scaleY:1});
            return;
        }// end function

        private function drawRollover() : void
        {
            this.rollover = new Sprite();
            this.rollover.graphics.beginFill(65280, 0);
            this.rollover.graphics.drawRect(0, 0, this.container.width, this.container.height);
            this.rollover.graphics.endFill();
            addChild(this.rollover);
            this.rollover.buttonMode = true;
            this.rollover.addEventListener(MouseEvent.ROLL_OVER, this.onThumbRollOver, false, 0, true);
            this.rollover.addEventListener(MouseEvent.ROLL_OUT, this.onThumbRollOut, false, 0, true);
            return;
        }// end function

        private function drawLoader() : void
        {
            this.loader = new Shape();
            this.loader.graphics.beginFill(16777215, 1);
            this.loader.graphics.drawRect(0, 0, this.container.width, this.container.height);
            this.loader.graphics.endFill();
            this.loader.width = 0;
            addChild(this.loader);
            return;
        }// end function

        public function set selected(param1:Boolean) : void
        {
            if (param1 == this._selected)
            {
                return;
            }// end if
            this._selected = param1;
            TweenMax.to(this, 0.3, {alpha:this._selected ? (1) : (this.UNSELECTED_ALPHA)});
            return;
        }// end function

    }
}
